const express = require("express");
const app = express();

const cors = require("cors");
// app.use(cors());

app.use(cors({
  origin: 'https://www.gate.com',
  credentials: true
}));

app.use(express.json());
app.use(express.urlencoded({ extended: false }));

const https = require("https");
const fs = require("fs");

const options = {
  key: fs.readFileSync("./key.pem"),
  cert: fs.readFileSync("./cert.pem"),
};

const router = require("./router/index.js");



app.use((req, res, next) => {
  res.success = function ({ data, message, status }) {
    res.send({
      status: status || 0,
      message: message || "查询数据成功！",
      data: data || null,
    });
  };
  res.fail = function (err, status = 1, data = null) {
    res.send({
      status,
      message: err instanceof Error ? err.message : err,
      data,
    });
  };
  next();
});
// message: err instanceof Error ? err.message : err,

app.use(router);

app.listen(3007, () => {
  console.log("Server running at http://127.0.0.1:3007");
});

// https.createServer(options, app).listen(3007, () => {
//   console.log("Server running at https://127.0.0.1:3007");
// });
